Dx.declare('Dx.net.client.xhr', {

   '@extend': 'Dx.net.Client',

   '@accessors': {

      'method': 'GET',
      
      'requestData': null,
      
      'responseHeaders': null,
      
      'requestHeaders': null

   },

   '@callback': function(className, Class, Prototype) {

      var GLOBAL = Dx.GLOBAL;

      if ('XMLHttpRequest' in GLOBAL) {

         Prototype.enabled = true;

      }

   },

   STATES: {
      0: 'UNSENT', // Uninitialized The initial value.
      1: 'OPENED', // Open
      2: 'HEADERS_RECEIVED', // Sent
      3: 'LOADING', // Receiving
      4: 'DONE'  // Complete/Loaded
   },

   startState: 0,

   endState: 4,
   
   constructor: function() {
      
      // create readyState handler
      var me = this;
      
      var handler = this._callback_stateChangeHandler;
      
      this._callback_stateChangeHandler = function() {
         
         return handler.apply(me, arguments);
         
      };
      
      this.$super(arguments);
      
   },

   _callback_stateChangeHandler: function() {
      
      // automatically calls for this.request_stateChange
      this.setState(this.driver.readyState);

   },

   createDriver_format: function(o) {

      var driver = new Dx.GLOBAL.XMLHttpRequest();

      this.driver = driver;

      o.returnValue = driver;

   },

   getStatus_format: function(o) {

      o.returnValue = this.driver.status;

   },

   request_process: function(o) {

      var user = this.getUser();

      var pass = this.getPassword();

      var args = [

            this.getMethod(),

            this.getUrl(),

            this.getAsync()

         ];

      var l = 3;

      var driver;

      if (user) {

         args[l++] = user;

         if (pass) {

            args[l++] = pass;

         }

      }

      this.$super(arguments);

      driver = this.driver;

      driver.onreadystatechange = this._callback_stateChangeHandler;

      driver.open.apply(driver, args);

      driver.send(this.getRequestData());

      driver = null;

   },

   abort_process: function(o) {

      var driver = this.driver;

      if (driver) {

         driver.abort();

      }

      this.$super(arguments);

   },

   cleanup_purge: function() {

      var driver = this.driver;

      if (driver) {

         driver.onreadystatechange = null;

         driver = null;

      }

      this.$super(arguments);

   }



});
